package com.cmwa.solution.sys.msg.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cmwa.solution.sys.msg.dao.IMessageOperationDao;
import com.cmwa.solution.sys.msg.dao.IMsgNoticeDao;
import com.cmwa.solution.sys.msg.dao.IMsgWaitSendDao;
import com.cmwa.solution.sys.msg.dao.ITransactionCalendarDao;
import com.cmwa.solution.sys.msg.service.IMessageOperationService;
import com.cmwa.solution.sys.msg.service.IMsgEmpConfService;
import com.cmwa.solution.sys.msg.service.IMsgPublicEmailService;
import com.cmwa.solution.sys.msg.vo.MsgConfVo;
import com.cmwa.solution.sys.msg.vo.MsgEmpConfVo;
import com.cmwa.solution.sys.msg.vo.MsgNoticeVo;
import com.cmwa.solution.sys.msg.vo.MsgPackVo;
import com.cmwa.solution.sys.msg.vo.MsgPublicEmailVo;
import com.cmwa.solution.sys.msg.vo.MsgWaitSendVo;

@Service
@SuppressWarnings({"rawtypes", "unchecked"})
public class MessageOperationServiceImpl implements IMessageOperationService{
	@Resource
	private IMessageOperationDao messageOperationDao;
	@Resource
	private IMsgWaitSendDao msgWaitSendDao;
	@Resource
	private IMsgNoticeDao MsgNoticeDao;
	@Resource
	private ITransactionCalendarDao transactionCalendarDao;
	@Resource
	private IMsgEmpConfService msgEmpConfService;
	@Resource
	private IMsgPublicEmailService msgPublicEmailService;
	
	
	public MsgConfVo queryMsgConfByMsgCode(String msgCode) {
		MsgConfVo msgConfVo = messageOperationDao.queryMsgConfByMsgCode(msgCode);
		if(msgConfVo != null){
			List<MsgEmpConfVo> emps = msgEmpConfService.queryConfEmpByConfId(msgConfVo.getId());
			
			Map<String, String> params=new HashMap<String, String>();
			params.put("CONF_ID", msgConfVo.getId());
			
			List<MsgPublicEmailVo> pubEmails=msgPublicEmailService.queryPublicEmailByConfId(params);
			
			MsgPackVo pack = new MsgPackVo();
			msgConfVo.setPackVo(pack);
			pack.addConf(msgConfVo);
			
			for(MsgEmpConfVo emp: emps){
				if(MsgEmpConfVo.EMP_TYPE_R.equals(emp.getEmpType())){
					List<MsgEmpConfVo> lst_emp_r = msgConfVo.getEmps();
					if(lst_emp_r==null){
						lst_emp_r=new ArrayList<MsgEmpConfVo>();
					}
					lst_emp_r.add(emp);
					msgConfVo.setEmps(lst_emp_r);
				}
				if(MsgEmpConfVo.EMP_TYPE_C.equals(emp.getEmpType())){
					List<MsgEmpConfVo> lst_emp_c = msgConfVo.getEmpsC();
					if(lst_emp_c==null){
						lst_emp_c=new ArrayList<MsgEmpConfVo>();
					}
					lst_emp_c.add(emp);
					msgConfVo.setEmpsC(lst_emp_c);
				}
			}
			
			for(MsgPublicEmailVo pub : pubEmails){
				if(MsgPublicEmailVo.EMAIL_TYPE_R.equals(pub.getEmailType())){
					List<MsgPublicEmailVo> lst_pub_email_r = msgConfVo.getPubEmailsR();
					if(lst_pub_email_r==null){
						lst_pub_email_r=new ArrayList<MsgPublicEmailVo>();
					}
					lst_pub_email_r.add(pub);
					msgConfVo.setPubEmailsR(lst_pub_email_r);
				}
				if(MsgPublicEmailVo.EMAIL_TYPE_C.equals(pub.getEmailType())){
					List<MsgPublicEmailVo> lst_pub_email_c = msgConfVo.getPubEmailsC();
					if(lst_pub_email_c==null){
						lst_pub_email_c=new ArrayList<MsgPublicEmailVo>();
					}
					lst_pub_email_c.add(pub);
					msgConfVo.setPubEmailsC(lst_pub_email_c);
				}
			}
			
			return msgConfVo;
		}
		
		return null;
	}
	
	@Override
	public MsgConfVo queryMsgConfById(String id) {
		MsgConfVo msgConfVo = messageOperationDao.queryMsgConfById(id);
		if(msgConfVo != null){
			MsgPackVo pack = new MsgPackVo();
			msgConfVo.setPackVo(pack);
			pack.addConf(msgConfVo);
			return msgConfVo;
		}
		return null;
	}



	@Override
	public List<MsgConfVo> queryAllMsgConf() {
		List<MsgConfVo> rst=new ArrayList<MsgConfVo>();
		
		List<MsgConfVo> res = messageOperationDao.queryAllMsgConf();
		
		List<MsgEmpConfVo> emps=msgEmpConfService.queryAllConfEmp();
		
		List<MsgPublicEmailVo> pubEmails=msgPublicEmailService.queryAllConfPublicEmail();
		
		for(MsgConfVo one : res){
			MsgPackVo pack = new MsgPackVo();
			one.setPackVo(pack);
			pack.addConf(one);
			for(MsgEmpConfVo emp: emps){
				if(emp.getMsgConfId().equalsIgnoreCase(one.getId()) && MsgEmpConfVo.EMP_TYPE_R.equals(emp.getEmpType())){
					List<MsgEmpConfVo> lst_emp_r = one.getEmps();
					if(lst_emp_r==null){
						lst_emp_r=new ArrayList<MsgEmpConfVo>();
					}
					lst_emp_r.add(emp);
					one.setEmps(lst_emp_r);
				}
				if(emp.getMsgConfId().equalsIgnoreCase(one.getId()) && MsgEmpConfVo.EMP_TYPE_C.equals(emp.getEmpType())){
					List<MsgEmpConfVo> lst_emp_c = one.getEmpsC();
					if(lst_emp_c==null){
						lst_emp_c=new ArrayList<MsgEmpConfVo>();
					}
					lst_emp_c.add(emp);
					one.setEmpsC(lst_emp_c);
				}
			}
			
			for(MsgPublicEmailVo pub : pubEmails){
				if(pub.getConfId().equalsIgnoreCase(one.getId()) && MsgPublicEmailVo.EMAIL_TYPE_R.equals(pub.getEmailType())){
					List<MsgPublicEmailVo> lst_pub_email_r = one.getPubEmailsR();
					if(lst_pub_email_r==null){
						lst_pub_email_r=new ArrayList<MsgPublicEmailVo>();
					}
					lst_pub_email_r.add(pub);
					one.setPubEmailsR(lst_pub_email_r);
				}
				if(pub.getConfId().equalsIgnoreCase(one.getId()) && MsgPublicEmailVo.EMAIL_TYPE_C.equals(pub.getEmailType())){
					List<MsgPublicEmailVo> lst_pub_email_c = one.getPubEmailsC();
					if(lst_pub_email_c==null){
						lst_pub_email_c=new ArrayList<MsgPublicEmailVo>();
					}
					lst_pub_email_c.add(pub);
					one.setPubEmailsC(lst_pub_email_c);
				}
			}
			
			rst.add(one);
		}
		return rst;
	}
	

	
	public int saveMsgWaitSendVo(MsgWaitSendVo vo){
		return msgWaitSendDao.create(vo);
	}
	
	public int saveMsgNoticeVo(MsgNoticeVo vo) {
		return MsgNoticeDao.create(vo);
	}
	@Override
	public List<String> queryMsgEmpConfById(String msgConfId) {
		return messageOperationDao.queryMsgEmpConfById(msgConfId);
	}
	
	@Override
	public List<Map<String, String>> queryMsgEmpConfByMsgCode(String msgCode) {
		return messageOperationDao.queryMsgEmpConfByMsgCode(msgCode);
	}
	
	@Override
	public List<Map<String, String>> obtainSpecialPerson(List<String> taskIds) {
		return messageOperationDao.obtainSpecialPerson(taskIds);
	}
	
	@Override
	public List<Map<String, String>> obtainEmailAddressByEmpIds(String[] empIds) {
		List<Map<String, String>> rst=new ArrayList<Map<String,String>>();
		List<Map<String, String>> tmp=messageOperationDao.obtainEmailAddressByEmpIds(empIds);
		if(tmp!=null && tmp.size()>0){
			for(Map m : tmp){
				rst.add((Map<String, String>)m);
			}
		}
		return rst;
	}

	@Override
	public String findTransactionCalendarByCurrentDate() {
		return transactionCalendarDao.findTransactionCalendarByCurrentDate();
	}

	@Override
	public List<Map<String, String>> queryMsgConfPubEmailsByMsgCode(String msgConfid) {
		return messageOperationDao.queryMsgConfPubEmailsByMsgCode(msgConfid);
	}

}
